package com.lee.reply.controller;

import com.lee.api.client.ArticleClient;
import com.lee.common.model.*;
import com.lee.reply.service.IArticleReplyService;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-10-06
 * Time: 12:35
 */
@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private ArticleClient articleService;

    @Resource
    private IArticleReplyService articleReplyService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("articleId") @NotNull Long articleId,
                            @ApiParam("帖子正文") @RequestParam("content") @NotNull String content) {
        // 1.获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_KEY);
        // 2.判断用户状态（是否禁言）
        if (user.getState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            // 返回错误信息
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 3.调用service查询并校验文章信息
        Article article = articleService.selectById(articleId);
        // 判断文章是否删除
        if (article == null || article.getDeleteState() == 1) {
            // 打印日志
            log.warn("帖子不存在 article id = "+articleId);
            // 返回错误信息
            return AppResult.failed(ResultCode.ERROR_IS_NULL);
        }
        // 判断是否结贴
        if (article.getState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_ARTICLE_STATUS+"article id = "+articleId);
            // 返回错误信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_STATUS);
        }
        // 4.构造回复信息
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId); // 主贴id
        articleReply.setPostUserId(user.getId()); // 发帖用户
        articleReply.setContent(content); // 回复内容
        // 5.调用service回复帖子
        articleReplyService.create(articleReply);
        // 6.返回结果
        return AppResult.success();
    }

    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId (
            @ApiParam("帖子Id") @RequestParam("articleId") @NotNull Long articleId) {
        // 1.调用service查询主帖信息
        Article article = articleService.selectById(articleId);
        // 判断文章是否删除
        if (article == null || article.getDeleteState() == 1) {
            // 打印日志
            log.warn("帖子不存在 article id = "+articleId);
            // 返回错误信息
            return AppResult.failed(ResultCode.ERROR_IS_NULL);
        }
        // 判断是否结贴
        if (article.getState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_ARTICLE_STATUS+"article id = "+articleId);
            // 返回错误信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_STATUS);
        }
        // 2.调用service查询主贴回复列表
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        // 经过查资料这段逻辑好像不需要
        if (articleReplies == null) {
            articleReplies = new LinkedList<>();
        }
        // 3.返回正确信息
        return AppResult.success(articleReplies);
    }


}
